# 在已有代码基础上添加以下内容
import akshare as ak
import pandas as pd

import logging

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger('Func')


# 新建一个股票信息获取函数
def fetch_stock_info(stock_code):
    """
    获取股票详细信息
    :param stock_code: 6位股票代码
    :return: 包含股票信息的字典
    """
    try:
        logger.info(f"开始获取股票信息: {stock_code}")
        # 调用akshare接口获取股票信息
        df = ak.stock_individual_info_em(symbol=stock_code)
        
        # 将DataFrame转换为字典
        result = {}
        for _, row in df.iterrows():
            item = row['item']
            value = row['value']
            # 特殊处理数值类型
            if isinstance(value, (int, float)):
                # 如果是数值且很大，可能是股本或市值，转换为亿单位
                if value > 1e8:
                    value = f"{value/1e8:.2f}亿"
                # 如果是日期数字
                elif item == '上市时间' and len(str(int(value))) == 8:
                    value = str(int(value))
                    value = f"{value[:4]}-{value[4:6]}-{value[6:8]}"
            result[item] = value
        result["name"] = result["股票简称"]
        logger.info(f"成功获取股票信息: {stock_code}")
        return result
    except Exception as e:
        logger.error(f"获取股票信息失败: {stock_code}, 错误: {str(e)}")
        return None

